package ua.kpi_java_training.tyshchenko.module2;

/**
 * Class shows position of every fish
 *
 * @author Vlad Tyshchenko
 * @version 1.1 3 June 2013
 */
public class Position implements Cloneable {
    private int coordinateX;
    private int coordinateY;
    private Habitat habitat;


    /**
     * Constructor generates randoms coordinate data.
     *
     * @param habitat
     */
    public Position(Habitat habitat) {
        AbstractRand rand = new RandomNumber();
        coordinateX = rand.getPosition(habitat.getWidth());
        coordinateY = rand.getPosition(habitat.getLength());
        this.habitat = habitat;
    }

    /**
     * Mock object
     */
    public Position(AbstractRand rand, Habitat habitat) {
        coordinateX = rand.getPosition(habitat.getWidth());
        coordinateY = rand.getPosition(habitat.getLength());
        this.habitat = habitat;
    }

    /**
     * Method increases coordinate Width by lengthStep
     *
     * @param lengthStep - amount steps
     * @return true if was a change positions
     */
    public boolean addCoordinateX(int lengthStep) {
        if (coordinateX + lengthStep < habitat.getWidth()) {
            coordinateX += lengthStep;
            return true;
        }
        return false;
    }

    /**
     * Method increases coordinate Length by lengthStep
     *
     * @param lengthStep - amount steps
     * @return true if was a change positions
     */
    public boolean addCoordinateY(int lengthStep) {
        if (coordinateY + lengthStep < habitat.getLength()) {
            coordinateY += lengthStep;
            return true;
        }
        return false;
    }

    /**
     * Method reduces coordinate Width by LengthStep
     *
     * @param lengthStep - amount steps
     * @return true if was a change positions
     */
    public boolean robCoordinateX(int lengthStep) {
        if (coordinateX - lengthStep >= 0) {
            coordinateX -= lengthStep;
            return true;
        }
        return false;
    }

    /**
     * Method reduces coordinate Length by LengthStep
     *
     * @param lengthStep - amount steps
     * @return true if was a change positions
     */
    public boolean robCoordinateY(int lengthStep) {
        if (coordinateY - lengthStep >= 0) {
            coordinateY -= lengthStep;
            return true;
        }
        return false;
    }

    /**
     * Methods checks position's location in hurdle place
     *
     * @return true if position is in hurdle
     */
    public boolean checkHurdle() {
        return !habitat.checkAvailableSpace(this);
    }

    public int getWidth() {
        return coordinateX;
    }

    public int getLength() {
        return coordinateY;
    }

    public boolean equals(Position position) {
        return (coordinateX == position.coordinateX && coordinateY == position.coordinateY);
    }

    public Position clone() {
        try {
            return (Position) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("Cloning not allowed.");
            return this;
        }
    }

}
